/* 
 * Shuriken is the legal property of its developers. Please refer to the
 * COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using Shuriken.Core.Shell;
using Shuriken.Core.Store;
using Shuriken.Core.Store.Items;
using Shuriken.Core.Utils;
using Shuriken.Base;

namespace Shuriken.Core.Store.Scanners
{
    public class FavoritesScanner : IScanner
    {
        private FileScannerHelper scanner;
        private List<ISubject>  itemlist;

        public FavoritesScanner()
        {
            scanner = new FileScannerHelper();

            scanner.Locations.Add(ShellUtils.folderFavorites);
            scanner.Locations.Add(ShellUtils.folderCommonFavorites);

            scanner.Level = FileScannerHelper.UNLIMITED_LEVEL; 
            scanner.Pattern.Add("*.url");

            itemlist = new List<ISubject>();
        }

        /// <summary>
        /// Adds the item to the store
        /// </summary>
        /// <param name="store">store to add the item</param>
        /// <param name="item">item to add to the store</param>
        /// <param name="ShellImageIndex">shell image index</param>
        /// <remarks>
        /// Adds the item to the store and also decides what is the most appropriate type of item for this input
        /// </remarks>
        //public void AddItemToStore(IStore store, string item, int ShellImageIndex)
        //{
        //    Shell32.FolderItem fItem = ShellUtils.GetFolderItemFromFile(item);
        //    string url = ShellUtils.GetFolderItemPath(fItem);

        //    if (url.IndexOf(Items.SearchUrlItem.PLACEHOLDER, 0) != -1)
        //    {
        //        SearchUrlItem urlItem = new SearchUrlItem(fItem, store);
        //        urlItem.ImageIndex = ImageStore.AddSystemImageListIndex(ShellImageIndex);
        //        store.AddStoreItem(urlItem);
        //    }
        //    else
        //    {
        //        UrlItem urlItem = new UrlItem(fItem, store);
        //        urlItem.ImageIndex = ImageStore.AddSystemImageListIndex(ShellImageIndex);
        //        store.AddStoreItem(urlItem);
        //    }
        //}

        /// <summary>
        /// Creates a url item
        /// </summary>
        /// <param name="item">url</param>
        /// <param name="ShellImageIndex">image index in system shell store</param>
        /// <returns>url item</returns>
        private ISubject CreateItem(string item, int ShellImageIndex)
        {
            Shell32.FolderItem fItem = ShellUtils.GetFolderItemFromFile(item);
            string url = ShellUtils.GetFolderItemPath(fItem);
            string place_holder = Shuriken.Core.Store.Items.SearchUrlItem.PLACEHOLDER;

            if (url.IndexOf(place_holder, 0) != -1)
            {
                SearchUrlItem urlItem = new SearchUrlItem(fItem);
                urlItem.Icon = ImageStore.AddSystemImageListIndex(ShellImageIndex);
                return urlItem;
            }
            else
            {
                UrlItem urlItem = new UrlItem(fItem);
                urlItem.Icon = ImageStore.AddSystemImageListIndex(ShellImageIndex);
                return urlItem; 
            }
        }

        /// <summary>
        /// Building an image list of control panel items 
        /// </summary>
        /// <returns>list of icon indexes of cpl items</returns>
        private Dictionary<string, int> BuidImageList()
        {
            Shuriken.Core.Shell.ShellManager manager = Shuriken.Core.Shell.ShellManager.Instance;

            List<ShellItem> favList = manager.GetShellItemsList(ShellAPI.CSIDL.CSIDL_FAVORITES, true);
            favList.AddRange(manager.GetShellItemsList(ShellAPI.CSIDL.CSIDL_COMMON_FAVORITES, true));

            Dictionary<string, int> imgDictionary = new Dictionary<string, int>();

            foreach (ShellItem link in favList)
            {
                if (!imgDictionary.ContainsKey(link.Path))
                {
                    imgDictionary.Add(link.Path, link.IconIndex);
                }
            }

            return imgDictionary;
        }

        #region IScanner Members

        /// <summary>
        /// List of items in this scanner
        /// </summary>
        public List<ISubject> Items
        {
            get { return itemlist; }
        }

        /// <summary>
        /// Scane the items
        /// </summary>
        public void Update()
        {
            itemlist.Clear();

            List<string> items = scanner.Scan();
            Dictionary<string, int> images = BuidImageList();

            int index = -1;

            foreach (string urllink in items)
            {
                try
                {
                    if (images.ContainsKey(urllink))
                    {
                        index = images[urllink];
                        ISubject item = this.CreateItem(urllink, index);
                        itemlist.Add(item);
                    }
                }
                catch
                {
                    continue;
                }

            }
        }

        #endregion

        #region IObject Members

        public string Name
        {
            get { return "Favorites"; }
        }

        public string Description
        {
            get { return "Scanner for scanning shell favorites folder."; }
        }

        public string Icon
        {
            get
            {
                return string.Empty;
            }
            set
            {
                
            }
        }

        #endregion
    }
}
